﻿using System;
using System.Collections.Generic;
using System.Linq;
using TavDom.Library.Core.Data;
using TavDom.Library.Core.Data.EntityFramework;
using TavDom.Library.Core.Utilities.Search;
using TavDom.Library.DataAccess.Abstract;
using TavDom.Library.DataAccess.Concrete.EntityFramework.ContextBase;
using TavDom.Library.Entities.Concrete;

namespace TavDom.Library.DataAccess.Concrete
{
    public class EfContentTypeDal : IContentTypeDal
    {
        public DatabaseContext Context;

        public EfContentTypeDal()
        {
            Context = Helpers.ContextHelper.GetContext();
        }

        readonly EfEntityRepositoryBase<ContentType> _efEntityRepositoryBase = new EfEntityRepositoryBase<ContentType>(Helpers.ContextHelper.GetContext());
        IQueryable<ContentType> IEntityRepository<ContentType>.GetAll()
        {
            return _efEntityRepositoryBase.GetAll();
        }

        public IQueryable<ContentType> GetAllWithUsers()
        {
            return Context.ContentTypes.Include("CreatedBy").Include("UpdatedBy");
        }
        public IQueryable<ContentType> Search(List<SearchFilter> filters)
        {
            var deleg = ExpressionBuilder.GetExpression<ContentType>(filters).Compile();
            return Context.ContentTypes.Where(deleg).AsQueryable();
        }

        public ContentType Get(ContentType entity)
        {
            return _efEntityRepositoryBase.Get(entity);
        }

        public ContentType GetById(int id)
        {
            return Context.ContentTypes.FirstOrDefault(e => e.Id == id);
        }

        public ContentType Add(ContentType entity)
        {
            entity.CreateDate = DateTime.Now;
            entity.UpdateDate = DateTime.Now;
            return _efEntityRepositoryBase.Add(entity);

        }

        public void Delete(ContentType entity)
        {
            _efEntityRepositoryBase.Delete(entity);
        }

        public ContentType Update(ContentType entity)
        {
            entity.UpdateDate = DateTime.Now;
            return _efEntityRepositoryBase.Update(entity);
        }
    }
}
